package com.qhnu.zxjx.configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: druid配置
 * @author: panpei
 * @date: 2019/11/14 10:05
 * @version: 1.0
 **/
@Configuration
public class DruidConfiguration {

    private Logger logger = LogManager.getLogger(getClass());

    @Value("${spring.datasource.druid.exclusions:*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*}")
    private String exclusions;

    @Value("${spring.datasource.druid.stat-view-servlet.url-pattern:/druid/*}")
    private String statViewServletUrlPattern;

    @Value("${spring.datasource.druid.stat-view-servlet.reset-enable:false}")
    private String statViewServletResetEnable;

    @Value("${spring.datasource.druid.stat-view-servlet.login-username:root}")
    private String statViewServletLoginUsername;

    @Value("${spring.datasource.druid.stat-view-servlet.login-password:root}")
    private String statViewServletLoginPassword;

    @Value("${spring.datasource.druid.stat-view-servlet.allow:}")
    private String statViewServletAllow;

    @Value("${spring.datasource.druid.stat-view-servlet.deny:}")
    private String statViewServletDeny;

    @ConditionalOnProperty(
            value = {"spring.datasource.druid.stat-view-servlet.enabled"},
            matchIfMissing = false)
    @Bean
    public ServletRegistrationBean druidServlet() {
        logger.info("init olddb Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), statViewServletUrlPattern);
        // IP白名单
        servletRegistrationBean.addInitParameter("allow", statViewServletAllow);
        // IP黑名单(共同存在时，deny优先于allow)
        servletRegistrationBean.addInitParameter("deny", statViewServletDeny);
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", statViewServletLoginUsername);
        servletRegistrationBean.addInitParameter("loginPassword", statViewServletLoginPassword);
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", statViewServletResetEnable);
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions", exclusions);
        return filterRegistrationBean;
    }
}
